﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AdminVilleModel;
/// <summary>
/// Summary description for DbTable_Customer
/// </summary>

public class DbTable_Customer
{
    AdminVilleEntities context;

    public DbTable_Customer()
    {
        context = new AdminVilleEntities();
    }

    public int InsertCustomer(int GUID, string Name, string Surname)
    {
        CUSTOMER c = new CUSTOMER();

        c.CustomerID = GUID;
        c.customerName = Name;
        c.customerSurname = Surname;

        try
        {
            context.AddToCUSTOMERs(c);
            context.SaveChanges();
            return 1;
        }
        catch
        {
            return 0;
        }
    }

    public int DeleteCustomer(int GUID)
    {
        var c1 = (from customer in context.CUSTOMERs
                  where customer.CustomerID == GUID
                  select customer).SingleOrDefault();

        try
        {
            context.CUSTOMERs.DeleteObject(c1);
            context.SaveChanges();
            return 1;
        }
        catch
        {
            return 0;
        }
    }

    public int UpdateCustomer(int GUID, string Name, string Surname)
    {
        var c1 = (from customer in context.CUSTOMERs
                  where customer.CustomerID == GUID
                  select customer).SingleOrDefault();


        if (c1 != null)
        {
            try
            {
                c1.customerName = Name;
                c1.customerSurname = Surname;
                context.SaveChanges();
                return 1;
            }
            catch
            {
                return 2;
            }
        }
        else
        {
            return 0;
        }

    }

    public CUSTOMER GetCustomer(int GUID)
    {
        var c1 = (from customer in context.CUSTOMERs
                  where customer.CustomerID == GUID
                  select customer).SingleOrDefault();
        
        return c1;
    }
}